#ifndef _QUERY_LOADER_H_
#define _QUERY_LOADER_H_

#include <fstream>

#include "Query.h"
#include "json/json.h"

class QueryHandler
{
private:
	void printQuery(Json::Value &node, Query *q);
	
	Query *parseQueryfromInput(Json::Value &root, Query *q);
	void parseOpsfromInput(Json::Value &root);

	Operator *findOperatorbyName(std::string name);

	void getOpsList(std::vector <OperatorNode *> &opsList, Query *q);
	void setOpsList(std::vector <OperatorNode *> :: iterator it, Query *q);

	bool checkSwap(std::vector <OperatorNode *> &opsList, std::vector <OperatorNode *> &opsListPrev);
	bool checkSwap(Operator *op1, Operator *op2);
	bool checkCommutativity(Operator *op1, Operator *op2);
	bool checkMovable(Operator *op);
	void checkIdempotency(std::vector <OperatorNode *> &opsList, Query *q);
	bool checkIdempotency(Operator *op, Query *q);
	bool checkIdempotency(Operator *op);

	std::vector <std::pair<Operator *, Operator *> > banned_permutations;
	void ban_pair(std::pair<Operator *, Operator *> p);

	void parseCost(Json::Value &root, Query *q);
public:
	QueryHandler();

	Query *loadQueryFromFile(std::string filename);
	bool printQuery(std::string filename, Query *query);
	void search(Query *query);
};

#endif
